package com.aid.winter.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.aid.winter.common.annotation.RequiredRole;
import com.aid.winter.common.response.Result;
import com.aid.winter.dto.request.ClothesRegisterRequest;
import com.aid.winter.dto.response.ClothesRegisterVO;
import com.aid.winter.dto.response.PageResult;
import com.aid.winter.service.ClothesRegisterService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 尺码登记管理控制器
 */
@RestController
@RequestMapping("/clothes/register")
@RequiredArgsConstructor
public class ClothesRegisterController {

    private final ClothesRegisterService registerService;

    /**
     * 登记尺码（仅学生可操作）
     */
    @PostMapping
    @RequiredRole(1)
    public Result<ClothesRegisterVO> register(@Validated @RequestBody ClothesRegisterRequest request) {
        Long studentId = StpUtil.getLoginIdAsLong();
        return Result.ok(registerService.register(studentId, request));
    }

    /**
     * 更新登记信息（仅学生可操作自己的登记）
     */
    @PutMapping("/{id}")
    @RequiredRole(1)
    public Result<ClothesRegisterVO> updateRegister(
            @PathVariable Long id,
            @Validated @RequestBody ClothesRegisterRequest request) {
        Long studentId = StpUtil.getLoginIdAsLong();
        return Result.ok(registerService.updateRegister(id, studentId, request));
    }

    /**
     * 获取登记详情
     */
    @GetMapping("/{id}")
    public Result<ClothesRegisterVO> getRegisterDetail(@PathVariable Long id) {
        return Result.ok(registerService.getRegisterDetail(id));
    }

    /**
     * 查询登记列表
     */
    @GetMapping("/list")
    public Result<PageResult<ClothesRegisterVO>> listRegisters(
            @RequestParam(required = false) Long styleId,
            @RequestParam(required = false) Long studentId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(registerService.listRegisters(styleId, studentId, pageNum, pageSize));
    }

    /**
     * 统计尺码数量
     */
    @GetMapping("/statistics")
    public Result<List<Map<String, Object>>> getSizeStatistics(
            @RequestParam Long styleId,
            @RequestParam(required = false) Long collegeId) {
        return Result.ok(registerService.getSizeStatistics(styleId, collegeId));
    }
}